/*
 * MainFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.wdzl.frames;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.List;

import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

import com.wdzl.biz.DeptBiz;
import com.wdzl.biz.EmpBiz;
import com.wdzl.entity.ConditionBean;
import com.wdzl.entity.Depart;
import com.wdzl.entity.Employee;

/**
 * @author __USER__
 */
public class MainFrame extends javax.swing.JFrame {

    private int page = 1;//当前页
    private int total = 0;//总页数
    private DeptBiz deptBiz = new DeptBiz();
    private EmpBiz biz = new EmpBiz();
    private ConditionBean condition = new ConditionBean();//封装查询条件
    private List<Employee> list = null;

    public void setCondition(ConditionBean condition) {
        this.condition = condition;
    }

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
    }

    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        jMenuItem3 = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        jMenuItem4 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
//        jMenu2 = new javax.swing.JMenu();
//        jMenuItem5 = new javax.swing.JMenuItem();
        //设置右键菜单
        jPopupMenu = new JPopupMenu();
        JMenuItem addItem = new JMenuItem("添加");
        KeyStroke ks2 = KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK);
        addItem.setAccelerator(ks2);
        addItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                addEmp();
            }
        });
        JMenuItem delItem = new JMenuItem("删除");
        delItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                delEmp();
            }
        });
        JMenuItem updateItem = new JMenuItem("修改");
        updateItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                updateEmp();
            }
        });
        JMenuItem searchItem = new JMenuItem("查询");
        searchItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                searchEmp();
            }
        });
        jPopupMenu.add(addItem);
        jPopupMenu.add(delItem);
        jPopupMenu.add(updateItem);
        jPopupMenu.add(searchItem);
        jPopupMenu.add(new JSeparator());
        jPopupMenu.addSeparator();
        JMenuItem impItem = new JMenuItem("导入");
        JMenuItem expItem = new JMenuItem("导出");
        jPopupMenu.add(impItem);
        jPopupMenu.add(expItem);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("楷体_GB2312", 1, 24));
        jLabel1.setText("职工管理系统");
        Object[] obj = deptBiz.getDeaprts().toArray();
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(obj));
        jComboBox1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                page = 1;
                Depart d = (Depart) jComboBox1.getSelectedItem();
                condition = new ConditionBean();
                condition.setDid(d.getId());
                MainFrame.this.setTableValue();
            }

        });
        //////////////////////////////////////////////////

        this.setTableValue();

        jScrollPane1.setViewportView(jTable1);
        jLabel2.setText("末页");
        jLabel2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub
                jLabel2.setForeground(Color.BLUE);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub
                jLabel2.setForeground(Color.BLACK);
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                page = total;
                MainFrame.this.setTableValue();
            }
        });
        jLabel3.setText("下一页");
        jLabel3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub
                jLabel3.setForeground(Color.BLUE);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub
                jLabel3.setForeground(Color.BLACK);
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                if (page == total) {
                    JOptionPane.showMessageDialog(null, "已经是最后一页了，亲", "分页消息", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                page++;
                MainFrame.this.setTableValue();
            }
        });
        jLabel4.setText("上一页");
        jLabel4.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub
                jLabel4.setForeground(Color.BLUE);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub
                jLabel4.setForeground(Color.BLACK);
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                if (page == 1) {
                    JOptionPane.showMessageDialog(null, "已经是第一页了", "分页消息", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                page--;
                MainFrame.this.setTableValue();
            }
        });
        jLabel5.setText("首页");
        jLabel5.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub
                jLabel5.setForeground(Color.BLUE);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub
                jLabel5.setForeground(Color.BLACK);
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                page = 1;
                MainFrame.this.setTableValue();
            }
        });
        jLabel6.setText("当前页：");

        jLabel7.setText(page + "/" + total);

        jButton1.setText("添加");

        jButton1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                addEmp();
            }
        });

        jButton2.setText("修改");
        jButton2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                updateEmp();
            }
        });
        jButton3.setText("删除");
        jButton3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                delEmp();
            }
        });
        jButton4.setText("查询");
        jButton4.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                searchEmp();
            }
        });

        jButton5.setText("刷新");
        jButton5.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setTableValue();
            }
        });

        jMenu1.setText("文件（F）");
        jMenu1.setMnemonic(KeyEvent.VK_F);
        jMenuItem1.setText("导入");
        jMenuItem1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser jfc = new JFileChooser();
                jfc.showDialog(null, "导入XML");
                File f = jfc.getSelectedFile();
                if(f==null){
                    return;
                }
                List xmlList = biz.parserXml(f);
                if (xmlList != null) {
                    JOptionPane.showMessageDialog(null, "文件导入成功！", "导出消息", JOptionPane.INFORMATION_MESSAGE);
                    list = xmlList;
                    imp(list);
                } else {
                    JOptionPane.showMessageDialog(null, "文件导入失败！", "导出消息", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        KeyStroke ks1 = KeyStroke.getKeyStroke(KeyEvent.VK_I, InputEvent.CTRL_MASK);
        jMenuItem1.setAccelerator(ks1);
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("导出");
        jMenuItem2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                JFileChooser jfc = new JFileChooser();
                jfc.setDialogType(JFileChooser.SAVE_DIALOG);
                jfc.showDialog(null, "导出XML");
                File f = jfc.getSelectedFile();
                if (biz.createXmlDocument(list, f.getPath())) {
                    JOptionPane.showMessageDialog(null, "文件导出成功！", "导出消息", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "文件导出失败！", "导出消息", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        jMenu1.add(jMenuItem2);
        jMenu1.add(jSeparator2);

        jMenuItem3.setText("同步数据库");
        jMenuItem3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                if (biz.sysnchrDB(list)) {
                    JOptionPane.showMessageDialog(null, "同步成功！", "消息", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "同步失败！", "消息", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        jMenu1.add(jMenuItem3);
        jMenu1.add(jSeparator3);

        jMenuItem4.setText("退出");
        jMenuItem4.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        jMenu1.add(jMenuItem4);
        jMenu1.add(jSeparator1);

        jMenuBar1.add(jMenu1);

//        jMenu2.setText("其他");

//        jMenuItem5.setText("关于我们");
//        jMenu2.add(jMenuItem5);

//        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
                getContentPane());
        getContentPane().setLayout(layout);
        layout
                .setHorizontalGroup(layout
                        .createParallelGroup(
                                javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                layout
                                        .createSequentialGroup()
                                        .addContainerGap()
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(
                                                                jScrollPane1,
                                                                javax.swing.GroupLayout.Alignment.TRAILING,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                496,
                                                                Short.MAX_VALUE)
                                                        .addGroup(
                                                                javax.swing.GroupLayout.Alignment.TRAILING,
                                                                layout
                                                                        .createSequentialGroup()
                                                                        .addComponent(
                                                                                jLabel1)
                                                                        .addGap(
                                                                                57,
                                                                                57,
                                                                                57)
                                                                        .addComponent(
                                                                                jComboBox1,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                101,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(
                                                                javax.swing.GroupLayout.Alignment.TRAILING,
                                                                layout
                                                                        .createSequentialGroup()
                                                                        .addGroup(
                                                                                layout
                                                                                        .createParallelGroup(
                                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                        .addGroup(
                                                                                                layout
                                                                                                        .createSequentialGroup()
                                                                                                        .addComponent(
                                                                                                                jLabel6)
                                                                                                        .addPreferredGap(
                                                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                                                        .addComponent(
                                                                                                                jLabel7))
                                                                                        .addGroup(
                                                                                                layout
                                                                                                        .createSequentialGroup()
                                                                                                        .addGap(
                                                                                                                30,
                                                                                                                30,
                                                                                                                30)
                                                                                                        .addComponent(
                                                                                                                jButton1)))
                                                                        .addGap(
                                                                                32,
                                                                                32,
                                                                                32)
                                                                        .addComponent(
                                                                                jButton2)
                                                                        .addGap(
                                                                                34,
                                                                                34,
                                                                                34)
                                                                        .addComponent(
                                                                                jButton3)
                                                                        .addGap(
                                                                                34,
                                                                                34,
                                                                                34)
                                                                        .addGroup(
                                                                                layout
                                                                                        .createParallelGroup(
                                                                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                                                        .addComponent(
                                                                                                jLabel5)
                                                                                        .addComponent(
                                                                                                jButton4))
                                                                        .addPreferredGap(
                                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                        .addGroup(
                                                                                layout
                                                                                        .createParallelGroup(
                                                                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                                                        .addGroup(
                                                                                                layout
                                                                                                        .createSequentialGroup()
                                                                                                        .addComponent(
                                                                                                                jLabel4)
                                                                                                        .addGap(
                                                                                                                18,
                                                                                                                18,
                                                                                                                18)
                                                                                                        .addComponent(
                                                                                                                jLabel3))
                                                                                        .addComponent(
                                                                                                jButton5))
                                                                        .addPreferredGap(
                                                                                javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                                        .addComponent(
                                                                                jLabel2)))
                                        .addContainerGap()));
        layout
                .setVerticalGroup(layout
                        .createParallelGroup(
                                javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                layout
                                        .createSequentialGroup()
                                        .addGap(15, 15, 15)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addGroup(
                                                                layout
                                                                        .createSequentialGroup()
                                                                        .addComponent(
                                                                                jComboBox1,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                        .addGap(
                                                                                6,
                                                                                6,
                                                                                6))
                                                        .addGroup(
                                                                layout
                                                                        .createSequentialGroup()
                                                                        .addComponent(
                                                                                jLabel1)
                                                                        .addGap(
                                                                                18,
                                                                                18,
                                                                                18)))
                                        .addPreferredGap(
                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(
                                                jScrollPane1,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                258,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(
                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel2)
                                                        .addComponent(jLabel5)
                                                        .addComponent(jLabel6)
                                                        .addComponent(jLabel7)
                                                        .addComponent(jLabel4)
                                                        .addComponent(jLabel3))
                                        .addPreferredGap(
                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jButton1)
                                                        .addComponent(jButton2)
                                                        .addComponent(jButton3)
                                                        .addComponent(jButton4)
                                                        .addComponent(jButton5))
                                        .addGap(17, 17, 17)));

        pack();
    }// </editor-fold>
    //GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }

    public void imp(List<Employee> list){
        setTableValue(list);
    }
    public void setTableValue(List<Employee> list){

        this.list = list;
        Object[][] value = biz.getTableValue(list);
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                value, new String[]{
                "编号", "姓名", "性别", "部门", "薪资", "邮箱", "手机号码"}) {
            boolean[] canEdit = new boolean[]{false, true, true, true, true,false,false};

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        //设置数据居中显示
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
        tcr.setHorizontalAlignment(JLabel.CENTER);
        jTable1.setDefaultRenderer(Object.class, tcr);
        //设置右键菜单
        jTable1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    jPopupMenu.show(jTable1, e.getX(), e.getY());
                }
            }
        });

        //设置性别列和部门列为下拉菜单
        TableColumn tc1 = jTable1.getColumn("性别");
        JComboBox cbx = new JComboBox();
        cbx.addItem("男");
        cbx.addItem("女");
        tc1.setCellEditor(new DefaultCellEditor(cbx));

        TableColumn tc2 = jTable1.getColumn("部门");
        jComboBox2 = new JComboBox();
        Object[] obj2 = deptBiz.getDeaprts().toArray();
        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(obj2));
        tc2.setCellEditor(new DefaultCellEditor(jComboBox2));

        jTable1.add(jPopupMenu);

        jLabel7.setText(page + "/" + total);
    }
    public void setTableValue() {
        System.out.println("------%%%%%====setTableValue:"+list);
        total = biz.getTotal(condition);

        list = biz.getEmps(page, condition);

        setTableValue(list);
    }

    //添加新员工
    public void addEmp() {
        com.wdzl.frames.AddFrame af = new com.wdzl.frames.AddFrame(MainFrame.this);
        af.setResizable(false);
        af.setTitle("添加新员工");
        af.setLocationRelativeTo(null);
        af.setVisible(true);
    }

    //删除员工
    public void delEmp() {
        if (jTable1.getSelectedRowCount() > 0) {
            int flag = JOptionPane.showConfirmDialog(null, "你确定要删除吗？");
            if (flag == 0) {
                int x[] = jTable1.getSelectedRows();
                String[] ids = new String[x.length];
                for (int i = 0; i < x.length; i++) {
                    String id = jTable1.getValueAt(x[i], 0).toString();
                    ids[i] = id;
                }
                int count = biz.delEmp(ids);
                if (count > 0) {
                    JOptionPane.showMessageDialog(null, count + "行删除成功！", "删除消息", JOptionPane.INFORMATION_MESSAGE);
                    MainFrame.this.setTableValue();
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "请选择要删除的行", "删除消息", JOptionPane.ERROR_MESSAGE);
        }
    }

    //修改员工
    public void updateEmp() {
        if (jTable1.getSelectedRowCount() > 0) {
            int x = jTable1.getSelectedRow();
            int id = Integer.parseInt(jTable1.getValueAt(x, 0).toString());
            String name = jTable1.getValueAt(x, 1).toString();
            String sex = jTable1.getValueAt(x, 2).toString();
            Depart d = (Depart) jComboBox2.getSelectedItem();
            int did = d.getId();
            double salary = Double.parseDouble(jTable1.getValueAt(x, 4).toString());

            Employee emp = new Employee(id, name, sex, did, salary);
            if (biz.updateEmp(emp) > 0) {
                JOptionPane.showMessageDialog(null, "修改成功！", "修改消息", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, "修改失败！", "修改消息", JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(null, "请选择要修改的行", "修改消息", JOptionPane.ERROR_MESSAGE);
        }
    }

    //查询员工
    public void searchEmp() {
        com.wdzl.frames.SearchFrame sf = new com.wdzl.frames.SearchFrame(MainFrame.this);
        sf.setResizable(false);
        sf.setLocationRelativeTo(null);
        sf.setVisible(true);
    }

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JMenu jMenu1;
//    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
//    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JTable jTable1;
    private JComboBox jComboBox2;
    private JPopupMenu jPopupMenu;
    // End of variables declaration//GEN-END:variables

}